// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var leftMenu = require("leftMenu/leftMenu1");
    var topMenu = require("topMenu/topMenu");
    var router = require("hash/pageRouter");
    var dialog = require("dialog");
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
    var init = function(num){
    	leftMenu.init(num);
    	topMenu.init(num);
    	$(".table-section").removeClass("none");
    	$(".footer").removeClass("none");
		initEvent();
		router.init();
		inquireDeviceList();
    };
	var initEvent = function(){
		/*新增的点击事件*/
        $("#leftMenu").on("click", "#accept-new-mission", function() {
            router.route("supervise_tally_new_accept_mission.html");
        });
        $("#leftMenu").on("click", "#accept-unfinished-mission", function() {
            router.route("accept-unfinished-mission.html");
        });
        $("#main").on("click", ".supervise-tatlly-table-data-operation a", function() {
            router.route("supervise_tally_new_accept_mission_detail.html");
        });

        //接受任务
         $("#main").on('click', '#accept-new-mission', function() {
            acceptMission();
        });
		//搜索按钮
        $(".table-search-button").on("click",function(){
            pageIndex = 1;
            inquireDeviceList();
        });
         //删除pad
        $("#deviceListBody").on('click', '.table_delete_btn', function() {
            var deviceId = $(this).parent().parent().attr("deviceId");
            var alias = $(this).parent().parent().attr("alias");
            dialog.showConfirmDialog("删除平板", "是否确定删除设备别名为" + alias + "平板?", function() {
                deleteDevice(deviceId);
            });
        });
    };
    var inquireDeviceList = function(){
        var keyword = $("#keyword").val();
        base.fnAjax({
            action: "inquireDeviceList.json",
            oData: {
                pageIndex: pageIndex,
                pageSize: pageSize,
                keyword:keyword
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                    var tpl = $("#deviceListTpl").html();
                    var html = juicer(tpl, data);
                    $("#deviceListBody").html(html);
                    totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl($("#pagenation"),
                        totalRecordCount,
                        data.pageIndex,
                        data.pageSize,
                        function(p) {
                            pageIndex = p;
                            inquireDeviceList();
                        });
                } else {
                    base.alert(2,"信息获取失败");
                }
            }
        });
    }

    //接收任务的接口调用
    var acceptMission = function() {
        var addItemDialog = $("#accept-mission-dialog");
        dialog.showDialog(addItemDialog, function() {
            var promptDialog = validate.validateInput(addItemDialog);
            if (promptDialog.length != 0) {
                util.alert(promptDialog);
            } else {
                var body = base.parseForm($("#accept-mission-dialog"));
                platformBase.fnAjax({
                    action: "createZoneContinent.json", //接口名称
                    oData: body, //传递参数
                    fnSuccess: function(data) { //成功回调
                        if (data.status == 200) {
                            oData.pageIndex = 1;
                            getContinentInfo();
                        } else {
                            util.alert(3,"增加洲信息失败！");
                        }
                        dialog.hideDialog(addItemDialog);
                    }
                });
            }
        });
    };

    return {
        init:init
    };
});